import { defineConfig, loadEnv } from 'vite'
import { fileURLToPath } from 'url'
import { vitePlugin } from './plugins'
import { useAuthRoute } from './src/utils/file'

const resolve = (dir: string) => fileURLToPath(new URL(dir, import.meta.url))

export default async function (configEnv) {
    useAuthRoute()
    const viteEnv = loadEnv(configEnv.mode, process.cwd())
    return defineConfig({
        server: {
            host: '0.0.0.0',
            port: 8090,
            open: true,
            cors: true,
            proxy: {
                '/mock': 'http://localhost:8090',
                '/api': {
                    target: viteEnv.VITE_BASE_URL,
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/api/, ''),
                },
            },
            fs: {
                strict: false,
            },
        },
        plugins: vitePlugin(configEnv, resolve),
        resolve: {
            alias: {
                '@': resolve('./src'),
                '~': resolve('./'),
            },
        },
        build: {
            terserOptions: {
                compress: {
                    //生产环境时移除console
                    drop_console: true,
                    drop_debugger: true,
                },
            },
            // 取消计算文件大小，加快打包速度
            reportCompressedSize: false,
            sourcemap: true,
        },
    })
}
